using MediatR;
using Microsoft.AspNetCore.Mvc;
using Medical.ErrorCode;
using BaseData.ErrorCode;
using Medical.ReadApi.Dto;
using Medical.WriteApi.Appcontext.Command;
using Medical.WriteApi.Dto;

namespace Medical.WriteApi.Controllers
{
	/// <summary>
	/// 员工管理控制器（写操作）
	/// </summary>
	[ApiController]
	[Route("api/[controller]/[action]")]
	public class EmployeeManagementController : ControllerBase
	{
		private readonly IMediator _mediator;
		
		/// <summary>
		/// 构造函数
		/// </summary>
		/// <param name="mediator">MediatR中介者</param>
		public EmployeeManagementController(IMediator mediator)
		{
			_mediator = mediator;
		}

		/// <summary>
		/// 创建员工
		/// </summary>
		/// <param name="command">创建员工命令，包含员工基本信息</param>
		/// <returns>创建成功的员工信息</returns>
		/// <remarks>
		/// 示例请求:
		/// POST /api/EmployeeManagement/Create
		/// {
		///   "userName": "doctor001",
		///   "fullName": "张医生",
		///   "workNo": "E001",
		///   "departmentId": 1,
		///   "email": "doctor@example.com",
		///   "phone": "13800138000",
		///   "role": "医生",
		///   "isActive": true
		/// }
		/// </remarks>
		/// <response code="200">创建成功，返回员工信息</response>
		/// <response code="400">请求参数错误</response>
		/// <response code="500">服务器内部错误</response>
		[HttpPost]
		[ProducesResponseType(typeof(ApiResult<EmployeeDto>), 200)]
		[ProducesResponseType(400)]
		[ProducesResponseType(500)]
		public async Task<ApiResult<EmployeeDto>> Create([FromBody] CreateEmployeeCommand command)
		{
			try { var data = await _mediator.Send(command); return new ApiResult<EmployeeDto> { Code = ApiEnum.新增成功, Message = "创建成功", data = data }; }
			catch (Exception ex) { return new ApiResult<EmployeeDto> { Code = ApiEnum.新增失败, Message = ex.Message, data = null }; }
		}

		/// <summary>
		/// 更新员工信息
		/// </summary>
		/// <param name="command">更新员工命令，包含员工ID和要更新的字段</param>
		/// <returns>更新后的员工信息</returns>
		/// <remarks>
		/// 示例请求:
		/// PUT /api/EmployeeManagement/Update
		/// {
		///   "employeeId": 1,
		///   "fullName": "新名称",
		///   "email": "newemail@example.com",
		///   "phone": "13900139000"
		/// }
		/// </remarks>
		/// <response code="200">更新成功，返回更新后的员工信息</response>
		/// <response code="400">请求参数错误</response>
		/// <response code="404">员工不存在</response>
		/// <response code="500">服务器内部错误</response>
		[HttpPut]
		[ProducesResponseType(typeof(ApiResult<EmployeeDto>), 200)]
		[ProducesResponseType(400)]
		[ProducesResponseType(404)]
		[ProducesResponseType(500)]
		public async Task<ApiResult<EmployeeDto>> Update([FromBody] UpdateEmployeeCommand command)
		{
			try { var data = await _mediator.Send(command); return new ApiResult<EmployeeDto> { Code = ApiEnum.更新成功, Message = "更新成功", data = data }; }
			catch (Exception ex) { return new ApiResult<EmployeeDto> { Code = ApiEnum.更新失败, Message = ex.Message, data = null }; }
		}

		/// <summary>
		/// 删除员工（逻辑删除）
		/// </summary>
		/// <param name="employeeId">员工ID</param>
		/// <param name="deletedBy">删除操作人（可选）</param>
		/// <returns>删除操作是否成功</returns>
		/// <remarks>
		/// 示例请求:
		/// DELETE /api/EmployeeManagement/Delete?employeeId=1&amp;deletedBy=admin
		/// 
		/// 注意：此操作是逻辑删除，不会真正删除数据库中的记录。
		/// </remarks>
		/// <response code="200">删除成功</response>
		/// <response code="400">请求参数错误</response>
		/// <response code="404">员工不存在</response>
		/// <response code="500">服务器内部错误</response>
		[HttpDelete]
		[ProducesResponseType(typeof(ApiResult<bool>), 200)]
		[ProducesResponseType(400)]
		[ProducesResponseType(404)]
		[ProducesResponseType(500)]
		public async Task<ApiResult<bool>> Delete([FromQuery] long employeeId, [FromQuery] string? deletedBy = null)
		{
			try { var ok = await _mediator.Send(new DeleteEmployeeCommand { EmployeeId = employeeId, DeletedBy = deletedBy }); return new ApiResult<bool> { Code = ApiEnum.删除成功, Message = "删除成功", data = ok }; }
			catch (Exception ex) { return new ApiResult<bool> { Code = ApiEnum.删除失败, Message = ex.Message, data = false }; }
		}

		/// <summary>
		/// 批量导入员工（会自动去重）
		/// </summary>
		/// <param name="employees">员工列表</param>
		/// <returns>成功导入的员工数量</returns>
		/// <remarks>
		/// 示例请求:
		/// POST /api/EmployeeManagement/BulkImport
		/// [
		///   {
		///     "userName": "doctor001",
		///     "fullName": "张医生",
		///     "workNo": "E001",
		///     "departmentId": 1,
		///     "role": "医生"
		///   },
		///   {
		///     "userName": "nurse001",
		///     "fullName": "李护士",
		///     "workNo": "E002",
		///     "departmentId": 1,
		///     "role": "护士"
		///   }
		/// ]
		/// 
		/// 注意：如果用户名或工号已存在，则跳过该员工（去重策略）。
		/// </remarks>
		/// <response code="200">导入完成，返回成功导入的数量</response>
		/// <response code="400">请求参数错误</response>
		/// <response code="500">服务器内部错误</response>
		[HttpPost]
		[ProducesResponseType(typeof(ApiResult<int>), 200)]
		[ProducesResponseType(400)]
		[ProducesResponseType(500)]
		public async Task<ApiResult<int>> BulkImport([FromBody] List<EmployeeDto> employees)
		{
			try { var count = await _mediator.Send(new BulkImportEmployeesCommand { Employees = employees }); return new ApiResult<int> { Code = ApiEnum.新增成功, Message = "导入成功", data = count }; }
			catch (Exception ex) { return new ApiResult<int> { Code = ApiEnum.新增失败, Message = ex.Message, data = 0 }; }
		}
	}
}
